﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Snake.Data
{
    class Sketching
    {
        #region Данные и конструкторы
        private Image _imageLevel;
        private Image _imageFood;
        private Image _imageSnake1;
        private Image _imageSnake2;

        DrawingGroup drawingGroupFood = new DrawingGroup();
        DrawingGroup drawingGroupLevel = new DrawingGroup();
        DrawingGroup drawingGroupSnake1 = new DrawingGroup();
        DrawingGroup drawingGroupSnake2 = new DrawingGroup();

        /// <summary>
        /// Передаем поверхности на отрисовку для одной змейки
        /// </summary>
        /// <param name="Level">Поверхность уровня</param>
        /// <param name="Food">Поверхность еды</param>
        /// <param name="Snake1">Поверхность змейки</param>
        public Sketching(Image Level, Image Food, Image Snake1)
        {
            _imageLevel = Level;
            _imageFood = Food;
            _imageSnake1 = Snake1;

            //Отрисовывамем границы поля для уровня
            SketchingBorder(drawingGroupLevel);
            //Отрисовываем границы поля для еды
            SketchingBorder(drawingGroupFood);
            //Отрисовываем границы поля для змейки
            SketchingBorder(drawingGroupSnake1);
        }

        /// <summary>
        /// Передаем поверхности на отрисовку для двух змеек
        /// </summary>
        /// <param name="Level">Поверхность уровня</param>
        /// <param name="Food">Поверхность еды</param>
        /// <param name="Snake1">Поверхность первой змейки</param>
        /// <param name="Snake2">Поверхность второй змейки</param>
        public Sketching(Image Level, Image Food, Image Snake1, Image Snake2)
        {
            _imageLevel = Level;
            _imageFood = Food;
            _imageSnake1 = Snake1;
            _imageSnake2 = Snake2;

            //Отрисовывамем границы поля для уровня
            SketchingBorder(drawingGroupLevel);
            //Отрисовываем границы поля для еды
            SketchingBorder(drawingGroupFood);
            //Отрисовываем границы поля для первой змейки
            SketchingBorder(drawingGroupSnake1);
            //Отрисовываем границы поля для второй змейки
            SketchingBorder(drawingGroupSnake2);
        }
        #endregion


        #region Метод отрисовки границ поля
        /// <summary>
        /// Отрисовка границ полей
        /// </summary>
        /// <param name="drawingGroup">Коллекция рисунков поля нуждающегося в отрисовке</param>
        private void SketchingBorder(DrawingGroup drawingGroup)
        {
            //Отрисовка рамки
            GeometryDrawing geometryDrawingBorder = new GeometryDrawing();
            RectangleGeometry rectangleGeometryDrawBorder = new RectangleGeometry();

            rectangleGeometryDrawBorder.Rect = new Rect(0, 0, 3, 3);
            geometryDrawingBorder.Geometry = rectangleGeometryDrawBorder;
            geometryDrawingBorder.Pen = new Pen(Brushes.Black, 0.02);

            drawingGroup.Children.Add(geometryDrawingBorder);
        }
        #endregion


        #region Уровень
        /// <summary>
        /// Заполнение уровня границами, разметкой и препятствиями
        /// </summary>
        /// <param name="obstacles">Массив препятствий</param>
        public void SketchingLevel(bool[,] obstacles)
        {
            //Отрисовка рамки рисунка
            GeometryDrawing geometryDrawingBorder = new GeometryDrawing();
            RectangleGeometry rectangleGeometryDrawBorder = new RectangleGeometry();

            rectangleGeometryDrawBorder.Rect = new Rect(0, 0, 3, 3);
            geometryDrawingBorder.Geometry = rectangleGeometryDrawBorder;
            geometryDrawingBorder.Pen = new Pen(Brushes.Black, 0.02);

            drawingGroupLevel.Children.Add(geometryDrawingBorder);
            

            //Отрисовка сетки
            GeometryDrawing geometryDrawingGridLines = new GeometryDrawing();

            GeometryGroup geometryGroupGridLines = new GeometryGroup();

            for (int i = 0; i < 31; i++)
            {
                LineGeometry lineGeometryGridLinesHorizontal = 
                    new LineGeometry(new Point(3.0, i * 0.1), new Point(0, i * 0.1));
                geometryGroupGridLines.Children.Add(lineGeometryGridLinesHorizontal);
            }

            for (int i = 0; i < 31; i++)
            {
                LineGeometry lineGeometryGridLinesVertical = 
                    new LineGeometry(new Point(i * 0.1, 3.0), new Point(i * 0.1, 0));
                geometryGroupGridLines.Children.Add(lineGeometryGridLinesVertical);
            }

            geometryDrawingGridLines.Geometry = geometryGroupGridLines;
            geometryDrawingGridLines.Pen = new Pen(Brushes.Gray, 0.006);

            drawingGroupLevel.Children.Add(geometryDrawingGridLines);


            //Отрисовка препятствий
            for (int i = 0; i < 30; i++)
                for(int j = 0; j < 30; j++)
                    if (obstacles[i, j])
                    {
                        GeometryDrawing geometryDrawingObstacle = new GeometryDrawing();
                        RectangleGeometry rectangleGeomtryObstacle = new RectangleGeometry();

                        rectangleGeomtryObstacle.Rect = new Rect(i * 0.1, j * 0.1, 0.1, 0.1);

                        geometryDrawingObstacle.Geometry = rectangleGeomtryObstacle;
                        geometryDrawingObstacle.Pen = new Pen(Brushes.Black, 0.006);
                        geometryDrawingObstacle.Brush = Brushes.Black;

                        drawingGroupLevel.Children.Add(geometryDrawingObstacle);

                    }

            _imageLevel.Source = new DrawingImage(drawingGroupLevel);
        }
        #endregion


        #region Еда
        /// <summary>
        /// Отрисовка еды
        /// </summary>
        /// <param name="_imageFood">Слой для отрисовки</param>
        /// <param name="x">Координата еды по X</param>
        /// <param name="y">Координата еды по Y</param>
        public void SketchingFood(int x, int y)
        {            
            //Отрисовка еды
            GeometryDrawing  geometryDrawingEllipse = new GeometryDrawing();
            EllipseGeometry ellipseGeometry = new EllipseGeometry();
            ellipseGeometry.Center = new Point(x*0.1+0.05, y*0.1+0.05);
            ellipseGeometry.RadiusX = 0.03;
            ellipseGeometry.RadiusY = 0.03;

            geometryDrawingEllipse.Geometry = ellipseGeometry;
            geometryDrawingEllipse.Pen = new Pen(Brushes.Red, 0.03);
            geometryDrawingEllipse.Brush = Brushes.Red;

            drawingGroupFood.Children.Add(geometryDrawingEllipse);
            
            _imageFood.Source = new DrawingImage(drawingGroupFood);
        }

        /// <summary>
        /// Удаление еды
        /// </summary>
        public void RemovingSketchingFood()
        {
            try
            {
                drawingGroupFood.Children.RemoveAt(1);
            }
            catch(ArgumentOutOfRangeException e)
            {
                MessageBox.Show("Значение выходит за пределы допустисых значений", "Ошибка");
            }
        }
        #endregion


        #region Змейка
        #region Первая змейка
        /// <summary>
        /// Начальная отрисовка змейки
        /// </summary>
        /// <param name="snake"></param>
        public void SketchingSnake1(bool[,] snake)
        {
            //Отрисовка змейки
            for (int i = 0; i < 30; i++)
            {
                for (int j = 0; j < 30; j++)
                {
                    if (snake[i, j])
                    {
                        GeometryDrawing geometryDrawingSnake1 = new GeometryDrawing();
                        RectangleGeometry rectangleGeomtrySnake1 = new RectangleGeometry();

                        rectangleGeomtrySnake1.Rect = new Rect(i * 0.1, j * 0.1, 0.1, 0.1);

                        geometryDrawingSnake1.Geometry = rectangleGeomtrySnake1;
                        geometryDrawingSnake1.Pen = new Pen(Brushes.Black, 0.006);
                        geometryDrawingSnake1.Brush = Brushes.GreenYellow;

                        drawingGroupSnake1.Children.Add(geometryDrawingSnake1);
                    }
                }
            }

            _imageSnake1.Source = new DrawingImage(drawingGroupSnake1);
        }

        /// <summary>
        /// Удаление хвоста змейки
        /// </summary>
        public void RemovingSketchingSnake1()
        {
            try
            {
                drawingGroupSnake1.Children.RemoveAt(1);
            }
            catch(ArgumentOutOfRangeException e)
            {
                MessageBox.Show("Значение выходит за пределы допустисых значений", "Ошибка");
            }
        }

        /// <summary>
        /// Добавление головы змейки
        /// </summary>
        /// <param name="X">Координата X</param>
        /// <param name="Y">Координата Y</param>
        public void AddSketchingSnake1(int X, int Y)
        {

        }
        #endregion
        #endregion
    }
}
